package com.pshcp.service;

import com.framework.base.PageList;
import com.framework.tool.CommonTool;
import com.framework.util.SystemUtil;
import com.github.pagehelper.PageHelper;
import com.pshcp.common.enums.CommonStatusEnum;
import com.pshcp.domain.dto.DoctorAnnouncementDTO;
import com.pshcp.domain.model.DoctorAnnouncement;
import com.pshcp.mapper.DoctorAnnouncementMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import tk.mybatis.mapper.entity.Example;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 医生公告表 服务实现类
 * </p>
 *
 * @author auto generator
 * @since 2022-11-02
 */
@Service
public class DoctorAnnouncementService {

    @Autowired
    private DoctorAnnouncementMapper doctorAnnouncementMapper;

    public List<DoctorAnnouncement> getDoctorAnnouncementByDoctorId(Long doctorId) {
        Example example = new Example(DoctorAnnouncement.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("doctorId", doctorId);
        criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        example.setOrderByClause(" created_time desc");
        List<DoctorAnnouncement> list = doctorAnnouncementMapper.selectByExample(example);
        return list;
    }

    public PageList<DoctorAnnouncement> list(DoctorAnnouncementDTO doctorAnnouncementDTO) {
        if (doctorAnnouncementDTO.getPageNum() != null && doctorAnnouncementDTO.getPageSize() != null) {
            PageHelper.startPage(doctorAnnouncementDTO.getPageNum(), doctorAnnouncementDTO.getPageSize());
        }
        Example example = new Example(DoctorAnnouncement.class);
        Example.Criteria criteria = example.createCriteria();
        if (doctorAnnouncementDTO.getStatus() != null) {
            criteria.andEqualTo("status", doctorAnnouncementDTO.getStatus());
        }else {
            criteria.andEqualTo("status", CommonStatusEnum.INIT.getCode());
        }
        if (doctorAnnouncementDTO.getDoctorId() != null) {
            criteria.andEqualTo("doctorId", doctorAnnouncementDTO.getDoctorId());
        }
        example.setOrderByClause(" created_time desc");
        List<DoctorAnnouncement> list = doctorAnnouncementMapper.selectByExample(example);
        return CommonTool.buildPageList(list);
    }

    public List<DoctorAnnouncement> select(DoctorAnnouncement doctorAnnouncement) {
        return doctorAnnouncementMapper.select(doctorAnnouncement);
    }

    public int insert(DoctorAnnouncement doctorAnnouncement) {
        doctorAnnouncement.setId(SystemUtil.getId());
        doctorAnnouncement.setStatus(CommonStatusEnum.INIT.getCode());
        doctorAnnouncement.setCreatedTime(new Date());
        return doctorAnnouncementMapper.insert(doctorAnnouncement);
    }

    public int update(DoctorAnnouncement doctorAnnouncement) {
        doctorAnnouncement.setUpdateTime(new Date());
        return doctorAnnouncementMapper.updateByPrimaryKeySelective(doctorAnnouncement);
    }

    public DoctorAnnouncement findById(Long id) {
        return doctorAnnouncementMapper.selectByPrimaryKey(id);
    }

    public List<DoctorAnnouncement> findByIds(List<Long> ids) {
        Example example = new Example(DoctorAnnouncement.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andIn("id", ids);
        return doctorAnnouncementMapper.selectByExample(example);
    }

    public int delById(Long id) {
        Example example = new Example(DoctorAnnouncement.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("id", id);

        DoctorAnnouncement doctorAnnouncement = new DoctorAnnouncement();
        doctorAnnouncement.setStatus(CommonStatusEnum.DEL.getCode());
        return doctorAnnouncementMapper.updateByExampleSelective(doctorAnnouncement, example);
    }

}
